﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using ShapeDemo.UC.WinProp;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ShapeDemo
{
    public class EllipseDrawer
    {
        public Canvas EllipseCanvas { get; set; }
        public List<ASPNETX_WPFEllipse> EllipseCollection { get; set; }

        private WinPropEllipse winEllipseProp;
        private WinPropCommon winCommonProp;

        ASPNETX_WPFEllipse elBody;
        private TMouse _MouseType;

        private ContextMenu cELMenu=new ContextMenu();

        public TMouse MouseTye
        {
            get
            {
                return _MouseType;
            }
            set
            {
                switch (value)
                {
                    case TMouse.Drawing:

                        EllipseCanvas.Cursor = Cursors.Cross;

                        foreach (ASPNETX_WPFEllipse item in EllipseCollection)
                        {
                            item.EllipseBody.Cursor = Cursors.Cross;
                        }

                        _MouseType = value;

                        RegistContextMenu();

                        break;
                    case TMouse.Point:

                        EllipseCanvas.Cursor = Cursors.Arrow;

                        foreach (ASPNETX_WPFEllipse item in EllipseCollection)
                        {
                            item.EllipseBody.Cursor = Cursors.Hand;
                            item.EllipseBody.MouseLeftButtonDown += new MouseButtonEventHandler(EllipseBody_MouseLeftButtonDown);
                        }

                        _MouseType = value;

                        RegistContextMenu();

                        break;
                }
            }
        }

        private bool IsEllipseInDrawing = false;
        public bool IsShiftDown { get; set; }

        public EllipseDrawer(Canvas canvasContent, WinPropEllipse winShapeProp, WinPropCommon winProp)
        {
            EllipseCollection = new List<ASPNETX_WPFEllipse>();
            
            EllipseCanvas = canvasContent;
            winEllipseProp = winShapeProp;
            winCommonProp = winProp;

            _MouseType = TMouse.Drawing;

            MenuItem miClear = new MenuItem();
            miClear.Header = "Clear";
            miClear.Click += new RoutedEventHandler(MenuItem_Redraw_Click);

            cELMenu.Items.Add(miClear);
            
            RegistContextMenu();

            EllipseCanvas.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(ellipseCanvas_MouseLeftButtonDown);
            EllipseCanvas.MouseMove += new System.Windows.Input.MouseEventHandler(ellipseCanvas_MouseMove);

            BindListBox();
        }

        /// <summary>
        /// 注册上下文菜单，主要在模式切换里用
        /// </summary>
        public void RegistContextMenu()
        {
            if (MouseTye == TMouse.Drawing)
            {
                EllipseCanvas.ContextMenu = cELMenu;
            }
            if (MouseTye == TMouse.Point)
            {
                EllipseCanvas.ContextMenu = null;
            }
        }

        public void Draw()
        { 
            
        }

        #region 上下文菜单的处理
        /// <summary>
        /// 上下文菜单重画按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Redraw_Click(object sender, RoutedEventArgs e)
        {
            if (IsEllipseInDrawing == true)
            {
                EllipseCanvas.Children.Remove(elBody.EllipseBody);

                elBody = ResourceCenter.NewEllipse();

                EllipseCanvas.Children.Add(elBody.EllipseBody);

                IsEllipseInDrawing = false;
            }
            else
            {
                MessageBox.Show("Draw first");
            }
        }
        #endregion

        #region 画圆
        private void ellipseCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                if (IsEllipseInDrawing == false)
                {
                    elBody = ResourceCenter.NewEllipse();

                    EllipseCanvas.Children.Add(elBody.EllipseBody);

                    elBody.P1 = e.GetPosition(EllipseCanvas);
                    IsEllipseInDrawing = true;

                    return;
                }
                if (IsEllipseInDrawing == true)
                {
                    elBody.P2 = e.GetPosition(EllipseCanvas);
                    IsEllipseInDrawing = false;

                    //添加基本属性
                    elBody.CommonProperty.Index = winCommonProp.tbIndex.Text;
                    elBody.CommonProperty.Content = winCommonProp.tbContent.Text;

                    EllipseCollection.Add(elBody);

                    BindListBox();

                    return;
                }
            }
        }

        private void ellipseCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (_MouseType == TMouse.Drawing)
            {
                if (IsEllipseInDrawing == true)
                {
                    Point p = e.GetPosition(EllipseCanvas);
                    elBody.P2 = p;

                    elBody.EllipseBody.SetValue(Canvas.LeftProperty, elBody.Left);
                    elBody.EllipseBody.SetValue(Canvas.TopProperty, elBody.Top);

                    winEllipseProp.X = elBody.Left;
                    winEllipseProp.Y = elBody.Top;

                    if (IsShiftDown == false)
                    {
                        elBody.EllipseBody.Width = elBody.Width;
                        elBody.EllipseBody.Height = elBody.Height;

                        winEllipseProp.Width = elBody.Width;
                        winEllipseProp.Height = elBody.Height;
                    }
                    if (IsShiftDown == true)
                    {
                        elBody.EllipseBody.Width = elBody.Width;
                        elBody.EllipseBody.Height = elBody.Width;

                        winEllipseProp.Width = elBody.Width;
                        winEllipseProp.Height = elBody.Width;
                    }
                }
            }
        }
        #endregion

        /// <summary>
        /// 绑定各层所添加的图形到列表
        /// </summary>
        public void BindListBox()
        {
            winCommonProp.lstHistory.Items.Clear();
            foreach (ASPNETX_WPFEllipse item in EllipseCollection)
            {
                winCommonProp.lstHistory.Items.Add(item.CommonProperty.Content);
            }
        }

        void EllipseBody_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            foreach (ASPNETX_WPFEllipse item in EllipseCollection)
            {
                item.EllipseBody.Fill = new SolidColorBrush(Colors.Blue);
            }

            Ellipse mellipse = (Ellipse)sender;

            foreach (ASPNETX_WPFEllipse item in EllipseCollection)
            {
                if (item.EllipseBody == mellipse)
                {
                    winEllipseProp.SetValue(item);
                    item.EllipseBody.Fill = new SolidColorBrush(Colors.Red);

                    winCommonProp.tbIndex.Text = item.CommonProperty.Index;
                    winCommonProp.tbContent.Text = item.CommonProperty.Content;
                }
            }
        }
    }
}
